#行政助理安排日程
from datetime import datetime, timedelta

# 定义每个人的空闲时间段
andrew_free_times = [("11:00", "15:00")]  # 24小时制字符串表示时间
joanne_free_times = [("12:00", "14:00"), ("15:30", "17:00")]
hannah_free_times = [("12:00", "12:30"), ("14:00", "18:00")]

# 将时间字符串转换为datetime对象
def parse_time(time_str):
    return datetime.strptime(time_str, "%H:%M")

# 检查两个时间段是否有重叠
def is_overlap(start1, end1, start2, end2):
    return max(start1, start2) <= min(end1, end2)

# 查找共同空闲时间
common_free_times = []
for andrew_start, andrew_end in andrew_free_times:
    andrew_start_dt = parse_time(andrew_start)
    andrew_end_dt = parse_time(andrew_end)
    for joanne_start, joanne_end in joanne_free_times:
        joanne_start_dt = parse_time(joanne_start)
        joanne_end_dt = parse_time(joanne_end)
        if is_overlap(andrew_start_dt, andrew_end_dt, joanne_start_dt, joanne_end_dt):
            for hannah_start, hannah_end in hannah_free_times:
                hannah_start_dt = parse_time(hannah_start)
                hannah_end_dt = parse_time(hannah_end)
                if is_overlap(max(andrew_start_dt, joanne_start_dt), min(andrew_end_dt, joanne_end_dt), hannah_start_dt, hannah_end_dt):
                    # 找到共同空闲时间的开始和结束时间，并确保至少有30分钟空闲
                    common_start = max(andrew_start_dt, joanne_start_dt, hannah_start_dt)
                    common_end = min(andrew_end_dt, joanne_end_dt, hannah_end_dt)
                    if (common_end - common_start).total_seconds() >= 1800:  # 1800秒即30分钟
                        # 添加一个30分钟的会谈时间段
                        meeting_start = common_start.strftime("%H:%M")
                        meeting_end = (common_start + timedelta(minutes=30)).strftime("%H:%M")
                        common_free_times.append((meeting_start, meeting_end))
                        break  # 找到一个时间段后退出内层循环

# 打印可用于安排会谈的时间段
for start, end in common_free_times:
    print(f"可用于安排三人进行会谈的时间段为：{start} pm - {end} pm")
